/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.unice.miage.m2.webservices.cl.xbay.service;

import com.ebay.services.client.ClientConfig;
import com.ebay.services.client.FindingServiceClientFactory;
import com.ebay.services.finding.FindItemsByKeywordsRequest;
import com.ebay.services.finding.FindItemsByKeywordsResponse;
import com.ebay.services.finding.FindingServicePortType;
import com.ebay.services.finding.PaginationInput;
import com.ebay.services.finding.SearchItem;
import fr.unice.miage.m2.webservices.cl.xbay.model.ThirdPartyProduct;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

/**
 *
 * @author Utilisateur
 */
@Stateless
@Path("thirdPartyProduct")
public class ThirdPartyProductFacadeREST {

    public ThirdPartyProductFacadeREST() {
    }

    @GET
    @Produces({"application/xml", "application/json"})
    public List<ThirdPartyProduct> findAll() {


        // Préparer la requete
        //
        ClientConfig config = new ClientConfig();
        config.setApplicationId("Unice2946-74b1-45c9-b495-a5f149c7805");
        FindingServicePortType serviceClient = FindingServiceClientFactory.getServiceClient(config);
        FindItemsByKeywordsRequest request = new FindItemsByKeywordsRequest();

        // Definir les mots clés
        //
        String tags = "ipad";
        request.setKeywords(tags);

        PaginationInput pi = new PaginationInput();
        pi.setDelimiter("10");
        pi.setEntriesPerPage(30);
        pi.setPageNumber(1);
        request.setPaginationInput(pi);

        // Lancer la requete
        //
        FindItemsByKeywordsResponse result = serviceClient.findItemsByKeywords(request);

        ArrayList<ThirdPartyProduct> productList = new ArrayList<ThirdPartyProduct>();

        // Parcourir la réponse
        //
        List<SearchItem> items = result.getSearchResult().getItem();
        for (SearchItem item : items) {

            // {"itemId", "title", "globalId", "subtitle", "primaryCategory", "secondaryCategory", "galleryURL", "viewItemURL", "charityId", "productId", "paymentMethod", "autoPay", "postalCode", "location", "country", "storeInfo", "sellerInfo", "shippingInfo", "sellingStatus", "listingInfo", "returnsAccepted", "galleryPlusPictureURL", "compatibility", "distance", "condition", "delimiter", "any"})
            // Créer un objet ThirdPartyObject
            //

            ThirdPartyProduct p = new ThirdPartyProduct();

            p.setName(item.getTitle());
            //*
            p.setId(item.getItemId());
            p.setDescr(item.getSubtitle());
            Double dPrice = item.getSellingStatus().getConvertedCurrentPrice().getValue();
            p.setPrice(dPrice.floatValue());
            p.setTags(tags);
            
            ArrayList<String> strs = new ArrayList<String>();
            for (String s : item.getGalleryPlusPictureURL()) {
                strs.add(s);
                System.out.println(s);
            }
            p.setImages(strs.toArray(new String[0]));
            
            //*/

            productList.add(p);

            System.out.println(item.getTitle());
        }

        return productList;
    }
}
